package com.practices2.util;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.StringWriter;
import java.io.Writer;

public class LogUtil {

	private static final String LOG_FILE_NAME = "com.practices";
	public static final String LOG_TAG = "com.practices";
	private static final String PracticesFileName = "com.practices.txt";

	private static boolean isDebug = true;

	public static String getCurrentActivityName(Context context) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
		return cn.getClassName();
	}

	public static void setDebug(boolean isDebug) {
		isDebug = isDebug;
	}

	private static boolean getDebug() {
		return isDebug;
	}

	public static void i(String tag, String info) {
		if (getDebug())
			Log.i(tag, info);
	}

	public static void w(String tag, String info) {
		if (getDebug())
			Log.w(tag, info);
	}

	public static void e(String tag, String info) {
		if (getDebug()) {
			Log.e(tag, info);
			saveCrashInfo2File(tag, info);
		}
	}

	public static void e(String tag, Exception e) {
		if (getDebug())
			saveCrashInfo2File(tag, e);
	}

	public static void i(String tag, int info) {
		if (getDebug())
			Log.i(tag, String.valueOf(info));
	}

	public static void i(String tag, double info) {
		if (getDebug())
			Log.i(tag, String.valueOf(info));
	}

	public static void i(String tag, long info) {
		if (getDebug())
			Log.i(tag, String.valueOf(info));
	}

	public static void i(String tag, float info) {
		if (getDebug())
			Log.i(tag, String.valueOf(info));
	}

	public static void e(Exception e) {

		if (getDebug()) {

			Log.e("NearMeStatistics", "Error occurred with " + e.getClass());
			saveCrashInfo2File("NearMeStatistics", e);

		}
	}

	private static void saveCrashInfo2File(String tag, String info) {

		try {
			String name = "my_practices.txt";
			String path = Environment.getExternalStorageDirectory().getPath() + File.separator + name;
			File file = new File(path);
			if (!file.exists()) {
				file.createNewFile();
			}
			RandomAccessFile raf = new RandomAccessFile(file, "rw");
			raf.seek(raf.length());
			raf.write(info.getBytes());
			raf.close();
		} catch (Exception e) {
			e(tag, "LogUtil-saveCrashInfo2File: " + e.toString());
		}
	}

	public static void saveCrashInfo2File(String tag, Throwable ex) {

		StringBuffer sb = new StringBuffer();
		Writer writer = new StringWriter();
		PrintWriter printWriter = new PrintWriter(writer);
		ex.printStackTrace(printWriter);
		Throwable cause = ex.getCause();
		while (cause != null) {
			cause.printStackTrace(printWriter);
			cause = cause.getCause();
		}
		printWriter.close();
		String result = writer.toString();
		sb.append(result);
		String exceptionInfo = sb.toString();

		Log.e(tag, exceptionInfo);

		try {
			
			String name = "my_practices.txt";
			String path = Environment.getExternalStorageDirectory().getPath() + File.separator + name;
			File file = new File(path);
			if (!file.exists()) {
				file.createNewFile();
			}
			RandomAccessFile raf = new RandomAccessFile(file, "rw");
			raf.seek(raf.length());
			raf.write(exceptionInfo.getBytes());
			raf.close();
			
		} catch (Exception e) {
			LogUtil.e(tag, "LogUtil-saveCrashInfo2File: " + e.toString());
		}
	}
}